package fr.enseirb.mycub.component.map.bike;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.location.Location;
import android.net.Uri;
import android.provider.BaseColumns;
import android.util.Log;
import android.widget.FrameLayout;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

import fr.enseirb.mycub.activity.bike.StationDetailsActivity;
import fr.enseirb.mycub.component.map.OverlayBundle;
import fr.enseirb.mycub.component.map.OverlayBundleItem;
import fr.enseirb.mycub.db.BikeDBConstants;
import fr.enseirb.mycub.db.StationsProvider;

public class BikeStationsOverlay extends OverlayBundle {

	private ArrayList<? extends OverlayBundleItem> items = new ArrayList<BikeStationOverlay>();
	static TextDrawable texts_not_empty = null;
	static TextDrawable texts_bike_empty = null;
	static TextDrawable texts_full_empty = null;
	static PinDrawable pin = null;
	static Paint mTextPaint = new Paint();
	static Paint mPinPaint = new Paint();
	static boolean mDrawText;

	int IMAGE_HEIGHT;

	public BikeStationsOverlay(Resources resources, BitmapDrawable drawable1, BitmapDrawable drawable2, BitmapDrawable drawable3,
			MapView mv, Context context) {
		//this(resources,drawable1,mv,context);
		super(drawable1, mv, context);

		mTextPaint.setAntiAlias(true);
		mTextPaint.setTextSize(19);
		mTextPaint.setTextAlign(Align.RIGHT);
		mTextPaint.setColor(Color.BLACK);
		mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);

		mPinPaint.setStyle(Paint.Style.FILL);
		//mPinPaint.setColor(Color.BLUE);
		mPinPaint.setAlpha(150);
		mPinPaint.setAntiAlias(true);

		BikeStationsOverlay.texts_not_empty = new TextDrawable(resources, drawable1.getBitmap());
		BikeStationsOverlay.texts_bike_empty = new TextDrawable(resources, drawable2.getBitmap());
		BikeStationsOverlay.texts_full_empty = new TextDrawable(resources, drawable3.getBitmap());
		boundCenterBottom(texts_not_empty);
		boundCenterBottom(texts_bike_empty);
		boundCenterBottom(texts_full_empty);
		pin = new PinDrawable();
		IMAGE_HEIGHT = texts_not_empty.getIntrinsicHeight();
		populate();
	}



	public void setItems(ArrayList<? extends OverlayBundleItem> list) {
		items = list;
		setLastFocusedIndex(-1);
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return (items.get(i));
	}

	public ArrayList<? extends OverlayBundleItem> getOverlayList() {
		return items;
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		if (!shadow) {
			if (mapView.getZoomLevel() >= 16) {
				mDrawText = true;
				setBalloonBottomOffset(IMAGE_HEIGHT);
			} else {
				mDrawText = false;
				setBalloonBottomOffset(0);
			}
			super.draw(canvas, mapView, shadow);
		}
	}

	@Override
	public int size() {
		return (items.size());
	}

	public ArrayList<OverlayBundleItem> getOverlaysFromCursor(
			ArrayList<Cursor> stationsCursors, Location location, int distanceFilter) {
		Cursor stationsCursor = stationsCursors.get(0);  
		ArrayList<OverlayBundleItem> overlays = new ArrayList<OverlayBundleItem>(
				stationsCursor.getCount());
		if (!stationsCursor.moveToFirst())
			return overlays;
		int latitudeColumn = stationsCursor
				.getColumnIndex(BikeDBConstants.KEY_LATITUDE);
		int longitudeColumn = stationsCursor
				.getColumnIndex(BikeDBConstants.KEY_LONGITUDE);
		int bikesColumn = stationsCursor
				.getColumnIndex(BikeDBConstants.KEY_BIKES);
		int slotsColumn = stationsCursor
				.getColumnIndex(BikeDBConstants.KEY_SLOTS);
		int idColumn = stationsCursor.getColumnIndex(BaseColumns._ID);
		BikeStationOverlay overlay;
		Location stationLocation = new Location("");
		float distance = 0;
		do {
			overlay = new BikeStationOverlay(new GeoPoint(stationsCursor
					.getInt(latitudeColumn), stationsCursor
					.getInt(longitudeColumn)), String.valueOf(stationsCursor
							.getInt(bikesColumn)), String.valueOf(stationsCursor
									.getInt(slotsColumn)), stationsCursor.getInt(idColumn));
			
			if (distanceFilter != 0 && location != null) {
				stationLocation.setLatitude(((double) stationsCursor
						.getInt(latitudeColumn)) * 1E-6);
				stationLocation.setLongitude(((double) stationsCursor
						.getInt(longitudeColumn)) * 1E-6);
				distance = location.distanceTo(stationLocation);
				if (distance > distanceFilter) {
					continue;
				}
			}
			overlays.add(overlay);
		} while (stationsCursor.moveToNext());
		Log.i("BikesOverlay", "--> overlays : "+overlays.size());
		return overlays;
	}



	@Override
	protected boolean onBalloonTap(int index, OverlayItem item) {
		Log.i("BikeStations ", "ON BALLOON TAP");
		Intent intent = new Intent(mContext, StationDetailsActivity.class)
		.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intent.setAction(Intent.ACTION_VIEW);
		intent.setData(Uri
				.withAppendedPath(StationsProvider.CONTENT_URI, String
						.valueOf(((BikeStationOverlay) item)
								.getId())));
		mContext.startActivity(intent);
		return true;
	}



	@Override
	protected FrameLayout createBalloonOverlayView() {
		Log.i("BikeStations ", "createBalloonOverlayView()");
		BalloonOverlayViewStationsBike<OverlayItem> b =  new BalloonOverlayViewStationsBike<OverlayItem>(getMapView().getContext(),
				viewOffset);
		return b;
	}

}